package fr.neyb.wesb.core.data.content.impl.sb;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import fr.neyb.wesb.core.data.content.Field;
import fr.neyb.wesb.core.data.content.impl.AbstractRecord;
import fr.neyb.wesb.core.data.descriptors.record.RecordDescriptor;

public class SBRecord extends AbstractRecord {

	private final List<Field> content;
	private final List<Field> externalContent;

	public SBRecord(List<Field> content, RecordDescriptor recordDescriptor) {
		super(recordDescriptor);
		this.content = new ArrayList<>(content);
		externalContent = Collections.unmodifiableList(this.content);
	}

	// public SBRecord(String[] content, RecordDescriptor recordDescriptor) {
	// super(recordDescriptor);
	// this.content = new ArrayList<>();
	// List<FieldDescriptor> fieldDescr = recordDescriptor.getFieldDescr();
	//
	// int min = Math.min(content.length, fieldDescr.size());
	// for (int i = 0; i < min; i++) {
	// this.content.add(new SBField(content[i], fieldDescr.get(i)));
	// }
	// externalContent = Collections.unmodifiableList(this.content);
	// }

	@Override
	public String toString() {
		return content.toString();
	}

	@Override
	public List<Field> getFields() {
		return externalContent;
	}

	@Override
	public Field getField(int index) {
		return content.get(index);
	}

	@Override
	public Field getField(String name) {
		int index = getRecordDescriptor().getIndexOf(name);
		return index == -1 ? null : content.get(index);
	}

}
